//
//  LXRxSwiftExtension.swift
//  LXMoyaHelper
//
//  Created by yoctech on 2021/8/6.
//

import Foundation
import Moya
import RxSwift
#if !COCOAPODS
    import LXMoyaHelper
#endif

public extension ObservableType where Element == Response {
    
    func mapObject<D: LXJsonMapper>(_ type: D.Type, atKeyPath keyPath: String? = nil) -> Observable<D> {
        return flatMap { Observable.just(try $0.mapObject(type, atKeyPath: keyPath)) }
    }
    
    func mapArray<D: LXJsonMapper>(_ type: D.Type, atKeyPath keyPath: String? = nil) -> Observable<[D]> {
        return flatMap { Observable.just(try $0.mapArray(type, atKeyPath: keyPath)) }
    }
}

public extension PrimitiveSequence where Trait == SingleTrait, Element == Response {

    func mapObject<D: LXJsonMapper>(_ type: D.Type, atKeyPath keyPath: String? = nil) -> Single<D> {
        return flatMap { Single.just(try $0.mapObject(type, atKeyPath: keyPath)) }
    }
    func mapArray<D: LXJsonMapper>(_ type: D.Type, atKeyPath keyPath: String? = nil) -> Single<[D]> {
        return flatMap { Single.just(try $0.mapArray(type, atKeyPath: keyPath)) }
    }

}
